﻿using System;

public class Iterator
{
    int m_LeftBoundary;
    int m_RightBoundary;

    int m_CurIteratorIndex;

    public Func<int, int> Shift;
    public Action<int> Action;
    /// <summary>
    /// range is [leftBoundary, rightBoundary]
    /// </summary>
    /// <param name="leftBoundary"></param>
    /// <param name="rightBoundary"></param>
    public Iterator(int leftBoundary, int rightBoundary)
    {
        m_LeftBoundary = leftBoundary;
        m_RightBoundary = rightBoundary;
    }

    public void Foreach(int start)
    {
        m_CurIteratorIndex = start;
        while (Condition())
        {
            Action(m_CurIteratorIndex);
            m_CurIteratorIndex = Shift(m_CurIteratorIndex);
        }
    }

    private bool Condition()
    {
        return m_CurIteratorIndex <= m_RightBoundary && m_CurIteratorIndex >= m_LeftBoundary;
    }
}
